import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/basic_information/component_management_ledger_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/filter/filter_bean.dart';
import 'package:enterprise_v6_app/app/data/entities/application/filter/filter_entities_bean.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';

///部件管理台账(审批)
class ComponentManagementLedgerApproveController extends GetxController {
  /// 筛选右侧栏控制Key
  final popRightScaffoldKey = GlobalKey<ScaffoldState>();

  /// 搜索使用key
  final GlobalKey<CustomEasyRefreshListState> globalKey = GlobalKey();

  /// 搜索字符串
  String? searchX = '';

  /// 筛选条件列表
  List<FilterBean> filterList = [];

  /// 状态筛选条件
  String? status = '';

  /// 类型
  String? type = '';

  /// 选择的项目开始时间
  String? projectBeginDate;

  /// 选择的项目结束时间
  String? projectEndDate;

  @override
  void onInit() {
    super.onInit();
    initData();
  }

  /// 初始化数据
  Future<void> initData() async {
    //是否合格
    final List<FilterEntitiesBean> typeList = [];
    typeList.add(FilterEntitiesBean(name: '检验', value: '2'));
    typeList.add(FilterEntitiesBean(name: '更换', value: '4'));
    typeList.add(FilterEntitiesBean(name: '检定', value: '3'));
    typeList.add(FilterEntitiesBean(name: '检修', value: '1'));
    //筛选控制bean
    final FilterBean filterBeanQualified = FilterBean(title: '类型', filterList: typeList);
    filterList.add(filterBeanQualified);
  }

  // 获取数据
  Future<List<ComponentManagementLedgerEntity>> componentManagementLedgerList(int index, int size, {String? search}) async {
    final res = await HttpUtil().generateGetRequest<List<ComponentManagementLedgerEntity>>(
      Interface.componentManagementLedgerList,
      queryParameters: {
        'pageNum': index,
        'pageSize': size,
        'name': searchX, //名称
        'auditStatus': status, //状态 1:待发起 2:待审批3：审批中 4：通过 5：驳回
        'type': type, //类型：1.检修 2.检验 3.检定 4.更换
        'isApprovalPage': 1, //是否查询审批数据 1是 0否
      },
    ).execute();
    List<ComponentManagementLedgerEntity>? list = [];
    res.onSuccess((result) {
      if (result.data != null) {
        list = result.data;
      }
    });
    return list!;
  }

  /// 筛选
  void screen() {
    popRightScaffoldKey.currentState?.openEndDrawer();
  }

  /// 筛选确认
  void screenConfirm(List<FilterBean> value) {
    filterList = value;
    status = '';
    type = '';
    for (var i in value) {
      if (i.chosenValue.isEmpty) {
        continue;
      }
      if (i.title == '类型') {
        type = i.chosenValue[0].value as String;
      }
    }
    update();
    refreshPage();
  }

  /// 搜索
  void refreshPage() {
    globalKey.currentState?.onRefresh();
  }

  /// 进入审批详情页面
  Future<void> toApprovalDetailsPage({required ComponentManagementLedgerEntity item}) async {
    Get.toNamed(AppRoutes.componentManagementLedgerDetailPage, arguments: {'id': item.id, 'isApproval': item.auditStatus == 2 || item.auditStatus == 3})
        ?.then((value) {
      if (value != null) {
        refreshPage();
      }
    });
  }
}
